<?php 
/**
 * 商品详细表管理模型
 */
class GoodsDetailModel extends Model{
	public $table = 'goods_detail';
	public $validate = array(
		array('intro','nonull','商品详细不能为空',2,3)
		); 
	public function add_ga_data($gid){
		if (!$this->create()) {
			return false;
		}
		// 商品详细
		$intro = $_POST['intro'];  
		// 售后服务
		$service = isset($_POST['service'])?$_POST['service']:'';
		// 上传的图册数组
		$p_data = Q('post.photo',array()); 
		// p($p_data);die;
		if ($p_data) {
			$img = new Image();
			foreach ($p_data as $m) { 
				$s_data[] = $img->thumb($m,str_replace('.', '_small.',$m),87,57,6);
				$m_data[] = $img->thumb($m,str_replace('.', '_medium.',$m),338,340,6);
				$b_data[] = $img->thumb($m,str_replace('.', '_big.',$m),500,500,6); 	
			}
			// p($s_data);die;
			$s_str = implode(',', $s_data);
			$m_str = implode(',', $m_data);
			$b_str = implode(',', $b_data);
		}
		$data = array(
			'small'=>isset($s_str)?$s_str:'',
			'medium'=>isset($m_str)?$m_str:'',
			'big'=>isset($b_str)?$b_str:'',
			'intro'=>$intro,
			'service'=>$service,
			'goods_gid'=>$gid
		); 
		// p($data);die;
		$this->data['goods_gid'] = $gid;
		return $this->add($data);
 	}
 	/**
 	 * 编辑
  	 */
 	public function m_edit_goods($gid){  
 		$intro = $_POST['intro'];
 		$service = isset($_POST['service'])?$_POST['service']:''; 
 		// 获取旧图片地址数组
 		$old_pic = K('GoodsDetail')->where("goods_gid='{$gid}'")->getField('medium',true);
		$old_pic_data = explode(',',$old_pic[0]);
		// 上传的图册数组 
		$p_data = isset($_POST['photo'])?$_POST['photo']:array(); 
		if (!($old_pic_data === $p_data)) {  
			foreach ($p_data as $k => $path) {
			if (!$path) {
					unset($p_data[$k]);
				} 
			}
			// p($p_data);
			$img = new Image(); 
			foreach ($p_data as $path) {
				$s_data[] = $img->thumb($path,str_replace('.', '_small.',$path),200,200,6);
				$m_data[] = $img->thumb($path,str_replace('.', '_medium.',$path),400,400,6);
				$b_data[] = $img->thumb($path,str_replace('.', '_big.',$path),600,600,6); 
			}
			// p($s_data);die; 
			if (isset($s_data)?$s_data:array()) {
				$s_str = implode(',', $s_data);
				$m_str = implode(',', $m_data);
				$b_str = implode(',', $b_data);
			} 
			$data = array(
				'small'=>isset($s_str)?$s_str:'',
				'medium'=>isset($m_str)?$m_str:'',
				'big'=>isset($b_str)?$b_str:'',
				'intro'=>$intro,
				'service'=>$service, 
				'goods_gid'=>$gid
			);
			$this->where("goods_gid='{$gid}'")->update($data);
		}

		$this->where("goods_gid='{$gid}'")->update();


 	}
}